using System;
using System.Runtime.InteropServices;

namespace LePcCtrlApp
{


    class MouseEventPacket
    {       
        static byte MOUSEEVENTF_LEFTDOWN = 0x01; /* left button down */
        static byte MOUSEEVENTF_LEFTUP = 0xFE;/* left button up */
        static byte MOUSEEVENTF_RIGHTDOWN = 0x04; /* right button down */
        static byte MOUSEEVENTF_RIGHTUP = 0xFB;/* right button up */
        static byte MOUSEEVENTF_MIDDLEDOWN = 0x02; /* middle button down */
        static byte MOUSEEVENTF_MIDDLEUP = 0xFD; /* middle button up */
        static byte MOUSEEVENTF_WHEELDOWN = 0x08; /* wheel button rolled */
        static byte MOUSEEVENTF_WHEELUP = 0xF7; /* wheel button rolled */
        public enum MouseKey
        {
            Left,middle,rigth,wheel
        }
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct MouseEvent
        {
            public int Size;
            public byte CmdCode;
            public byte BtnMask;
            public short X;
            public short Y;
        };
        private MouseEvent m_MouseEvent;
        public MouseEventPacket()
        {
            m_MouseEvent = new MouseEvent();
            m_MouseEvent.Size =Marshal.SizeOf(m_MouseEvent);
            m_MouseEvent.CmdCode = 3;
        }
        public void MakeMouseEvent(byte Mask,short x,short y)
        {
            m_MouseEvent.BtnMask = Mask;
            m_MouseEvent.X = x;
            m_MouseEvent.Y = y;
        }
        public void UpAllKey()
        {
            m_MouseEvent.BtnMask = 0;
            m_MouseEvent.X= m_MouseEvent.Y=0;
        }

        public void Move(short x, short y)
        {
            MakeMouseEvent(0,x, y);
        }

        public void KeyDown(MouseKey key)
        {
            switch(key)
            {
                case MouseKey.Left:
                    m_MouseEvent.BtnMask |= MOUSEEVENTF_LEFTDOWN;
                    break;
                case MouseKey.middle:
                    m_MouseEvent.BtnMask |= MOUSEEVENTF_MIDDLEDOWN;
                    break;
                case MouseKey.rigth:
                    m_MouseEvent.BtnMask |= MOUSEEVENTF_RIGHTDOWN;
                    break;
                case MouseKey.wheel:
                    m_MouseEvent.BtnMask |= MOUSEEVENTF_WHEELDOWN;
                    break;
            }
        }
        public void KeyUp(MouseKey key)
        {
            switch (key)
            {
                case MouseKey.Left:
                    m_MouseEvent.BtnMask &= MOUSEEVENTF_LEFTUP;
                    break;
                case MouseKey.middle:
                    m_MouseEvent.BtnMask &= MOUSEEVENTF_MIDDLEUP;
                    break;
                case MouseKey.rigth:
                    m_MouseEvent.BtnMask &= MOUSEEVENTF_RIGHTUP;
                    break;
                case MouseKey.wheel:
                    m_MouseEvent.BtnMask &= MOUSEEVENTF_WHEELUP;
                    break;
            }
        }
        public byte[] GetMouseEventPacket()
        {
            return Struct2Bytes.StructToBytes(m_MouseEvent);
        }
    }
}